package com.china08.yxyapi.api.home.weikecheng;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.entity.mg.user.YxyUserFavor;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.repository.mg.YxyUserFavorRepository;
import com.china08.yxyapi.service.yxweike.YxyWeikeService;
import com.china08.yxyapi.util.EsResourceUpdateUtil;

/**
 * 收藏微课---适用于各种角色
 * 
 */
@CrossOrigin
@RestController
public class WeikeFavController extends YxyApiBaseController {

    @Autowired
    private YxyWeikeService weikeService;

    @Autowired
    private YxyUserFavorRepository favorRepository;

    @Autowired
    private YxyResResourceRepository weikeRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/home/weike/fav/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel,
            RespModel respModel) {
        reqModel.check();
        String userId = getProfile(request, response).getId();
        YxyUserFavor favor = favorRepository.findByResIdAndUserId(reqModel.weikeId, userId);
        YxyResResource resourceWk = weikeRepository.findById(reqModel.weikeId);
        if (favor == null) {
            favor = new YxyUserFavor();
            favor.setUserId(userId);
            favor.setResId(reqModel.weikeId);
            favor.setResType("01");// 微课
            ElasticYxyResResource resource = weikeService.findOneResource(reqModel.weikeId);
            favor.setResName(resource.getTitle());// 微课名称
            favor.setPhase(resource.getStage());
            favor.setSubject(resource.getWeikeSubject());
            favorRepository.save(favor);
            respModel.setMessage("收藏成功");
            resourceWk.setFavoriteCount(resourceWk.getFavoriteCount() + 1);
        } else {
            favorRepository.deleteByResIdAndUserId(reqModel.weikeId, userId);
            respModel.setMessage("取消收藏成功");
            Long cLong = resourceWk.getFavoriteCount() - 1 > 0 ? (resourceWk.getFavoriteCount() - 1) : 0l;
            resourceWk.setFavoriteCount(cLong);
        }
        weikeRepository.save(resourceWk);
        EsResourceUpdateUtil.updateEsResoure(reqModel.weikeId);
        return respModel;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

        public String weikeId;// 微课id

        public String stage;// 学段

        public String subject;// 学科

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(weikeId, null), "微课id不能为空");
        }
    }

    protected static class RespModel extends YxyApiRespModel {

    }

}
